<template>
  <div class="commonreadinfo">
    <commonreadtop :items="datatop" :activenum="Pnum" @shareshow="showshare" :adver_info="adver_info" :qr_code="qr_code"></commonreadtop>
    <div class="actioncontainer" v-if="datatop.length||items.length">
      <ul class="comreadul">
        <CommonReadStatus :item="Item" v-for="(Item,$index) in items" :lastidcomd="lastid" :key="$index" :comonreadstlast="comonreadstlast"></CommonReadStatus>
      </ul>
    </div>
    <div class="noaction" v-else>
      <div class="noactionicon"><span class="iconfont">&#xe667;</span></div>
      <div class="noactiontext">你还没有参加共读活动</div>
    </div>
    <transition
      name="sharemode">
      <Share v-if="isshare" @hide="hideshare" :show="3"></Share>
    </transition>
  </div>
</template>

<script>

  import {Toast} from 'mint-ui'

  export default {
    name: 'index',
    data: function () {
      return {
        Pnum: 0,
        touxiang: '',
        datatop: [],
        items: [],
        isshare: false,
        isAction: false,
        textbtn: '继续共读',
        textbtn1: '分享成就',
        btnstyle: {
          opacity: 1,
          width: '80%',
          boxShadow: '0 10px 24px 0 rgba(255, 90, 95, 0.4)',
          height: '4.5rem',
          background: '#ff5a5f'
        },
        adver_info:'',
        qr_code:'',
        lastid:'',
        comonreadstlast:''
      }
    },
    mounted: function () {
      this.updata()
      this.initlookmore()
    },
    methods: {
      updata: function () {
        // 获取用户活动列表共有4种状态都要获取然后添加在一起s
        var data1 = {
          activity_status: 0,
          page_index: 1
        }
        this.$axios.user_activity(data1).then(res => {
          this.Pnum = res.data.qing_data.code_list.length
          // 重新整理邀请码数据
          var newArr = [], uid, n = -1
          res.data.qing_data.code_list.map((item) => {
            if (uid === item.user_activity_id) {
              newArr[n].childrenl.push({invite_code_id: item.invite_code_id, activation_code: item.activation_code})
            } else {
              n++
              uid = item.user_activity_id
              newArr.push({
                user_activity_id: item.user_activity_id,
                vip_activity_name: item.vip_activity_name,
                childrenl: [{invite_code_id: item.invite_code_id, activation_code: item.activation_code}]
              })
            }
          })
          this.datatop = newArr
        })
        var data2={
          activity_status:1,
          page_index:1
        }
        this.$axios.user_activity(data2).then(res => {
          res.data.qing_data.list.map((item)=>{
            var timearr = item.activity_start_time.split(' ')[0].split('-')
            this.items.push({user_activity_id:'',activity_name:item.vip_activity_name,start_date:timearr[0]+'年'+timearr[1]+'月'+timearr[2]+'日',state:4,startday:item.activation_total})
          })
          this.initlookmore()
        })
        var data3={
          activity_status:2,
          page_index:1
        }
        this.$axios.user_activity(data3).then(res => {
          res.data.qing_data.activity_list.map((item)=>{
            this.items.push({user_activity_id:'',activity_name:item.activity_name,start_date:item.start_date,state:3,startday:3,user_list:item.user_list})
          })
          this.initlookmore()
        })
        var data4={
          activity_status:3,
          page_index:1
        }
        this.$axios.user_activity(data4).then(res => {
          res.data.qing_data.list.map((item)=>{
            this.items.push({id:item.post_id,activity_name:item.activity_name,state:item.scholar_status,startday:3})
          })
          if(this.items[this.items.length-1]){
            this.lastid = this.items[this.items.length-1].id
          }
          this.initlookmore()
        })

        // this.lastid = this.items[this.items.length-1].id
        // 获取用户活动列表数据e

        // 获取广告列表
        var adver={
          adver_type:8
        }
        this.$axios.adver_list(adver).then(res => {
          this.adver_info = res.data.qing_data.adver_info
          this.qr_code = res.data.qing_data.qr_code
        })
      },
      // 获取上次显示更多的记录结果
      initlookmore: function () {
        let comonreadstats = JSON.parse(localStorage.getItem('commonread_stats_showmore'))
        this.items.map((item) => {
          if (comonreadstats) {
            if (comonreadstats[item.id]) {
              if(comonreadstats[this.lastid]){
                this.comonreadstlast = comonreadstats[this.lastid]
              }
              item.showmore = false
            } else {
              item.showmore = true
            }
          } else {
            item.showmore = true
          }
        })
      },
      showshare: function (child) {
        var _this = this
        var shareparms = {
          post_id: child.invite_code_id,
          share_type: 28,
          share_page:-1
        }
        this.$axios.share_link_info(shareparms).then(res => {
          var data = {
            title: res.data.qing_data.title,
            desc: res.data.qing_data.desc,
            link: res.data.qing_data.link,
            imgUrl: res.data.qing_data.img
          }
          var obj = {}
          obj.share_id = res.data.qing_data.share_id
          obj.share_token = res.data.qing_data.share_token
          this.$wx.ShareTimeline(data, res => {
            obj.share_mode = res.share_mode
            obj.share_type = 22
            _this.share_done(obj)
          })
          this.$wx.ShareAppMessage(data, res => {
            obj.share_mode = res.share_mode
            obj.share_type = 22
            _this.share_done(obj)
          })
        })
        this.isshare = true
      },
      share_done: function (obj) {
        this.$axios.share_done(obj).then(res => {
          this.isshare = false
          if (res.data.qing_code !== 100) {
            Toast({
              message: '分享失败',
              iconClass: 'iconfont icon-bangdingshibai',
              duration: 5000
            })
          } else {
            Toast({
              message: '分享成功',
              iconClass: 'iconfont icon-tianjiachenggong',
              duration: 5000
            })
          }
        })
      },
      hideshare: function () {
        this.isshare = false
      }
    },
    components: {
      commonreadtop: () => import('../ComreadInvitecode/index'),
      btn: () => import('../../components/btn/index'),
      Share: () => import('../../components/Share/index'),
      CommonReadStatus: () => import('../../components/CommonReadStatus/index')
    }
  }
</script>

<style scoped>
  @import "../../assets/css/views/ComreadAction/index.css";


</style>
